require 'c:/tools/kronos/ruby/line'
require 'c:/tools/kronos/ruby/state'
require 'c:/tools/kronos/ruby/merge'
require 'c:/tools/kronos/ruby/pseudostate'
require 'c:/tools/kronos/ruby/formalstate'
require 'c:/tools/kronos/ruby/masters'

class MultiPageFSM

	attr_accessor :lines
	attr_accessor :states
	attr_accessor :pseudo_states
	attr_accessor :formal_states
	attr_accessor :masters

	# Will have an array of the "Shapes" element of each page
	def initialize( pages, masters )

		@lines			   = Array.new
		@states			   = Array.new
		@pseudo_states = Array.new
		@masters			 = masters
		
		pages.each { |page, index|

			pageData = loadFSM( page )

			pageData[:lines].each { |line|
				line.consolidateLineNames pageData[:states], pageData[:lines]
			}

			pageData[:lines].each { |line|
				line.consolidateMerging pageData[:states], pageData[:lines]
			}

			pageData[:pseudos].each { |pseudo|
				pseudo.consolidatePseudostateNames pageData[:states], pageData[:lines]
			}

			@lines.concat 					pageData[:lines]
			@states.concat 					pageData[:states]
			@pseudo_states.concat 	pageData[:pseudos]

			pageData.clear

		}

		@formal_states = FormalStateController.new @lines

	end

	def loadFSM( xmlString )

		hashTable = Hash.new

		hashTable[:states]    = Array.new
		hashTable[:lines]  	  = Array.new
		hashTable[:pseudos]   = Array.new

		xmlString.elements.each { | element |

			case element.attributes["Master"]
				when @masters.stateID
					hashTable[:states].push State.new element
				when @masters.pseudoID
					hashTable[:pseudos].push PseudoState.new element
				when @masters.connectorID
					hashTable[:lines].push Line.new element
			end

		}

		return hashTable

	end

end
